package com.example.neusoft.yuejian_12.Activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.neusoft.yuejian_12.Fragment.AskFragment;
import com.example.neusoft.yuejian_12.Fragment.VideoFragment;
import com.example.neusoft.yuejian_12.JavaBean.Master;
import com.example.neusoft.yuejian_12.Listener.MasterCollectListener;
import com.example.neusoft.yuejian_12.Listener.MasterDetailListener;
import com.example.neusoft.yuejian_12.Listener.MasterStateListener;
import com.example.neusoft.yuejian_12.Listener.UserNoCollectListener;
import com.example.neusoft.yuejian_12.Model.MasterModel;
import com.example.neusoft.yuejian_12.Model.UserModel;
import com.example.neusoft.yuejian_12.R;
import com.squareup.picasso.Picasso;

/**
 * Created by admin on 2017/9/30.
 */

public class MasterDetailActivity extends AppCompatActivity {

    private VideoFragment videoFragment;
    private AskFragment askFragment;
    private FragmentManager fragmentManager;
    private FragmentTransaction transaction;
    private Master info;
    private String coresult;
    private RadioGroup radioGroup;
    private String id;
    private TextView name,intro,maddress;
    private ImageView pic,back,col;
    private  String realname,thumb,introduction,province_name,city_name;
    SharedPreferences sp;
    private  String userid;
    Context context;
    MasterDetailListener masterDetailListener=new MasterDetailListener() {
        @Override
        public void onResponse(Master listBean) {
            info=listBean;
            initData();
        }

        @Override
        public void onFail(String msg) {

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_masterdetail);
        final Bundle bundle = getIntent().getExtras();
        id = bundle.getString("id");
        userid = getApplication().getSharedPreferences("userInfo", Context.MODE_PRIVATE).getString("user_id", String.valueOf(0));
        getMasterState();
        MasterModel model = new MasterModel();
        model.getdetail(id, masterDetailListener);
        initViews();
        initEvents();

    }
    public void initViews()
    {

        name=(TextView)findViewById(R.id.cname);
        pic=(ImageView)findViewById(R.id.cpic);
        intro=(TextView)findViewById(R.id.intro);
        radioGroup=(RadioGroup)findViewById(R.id.tea_detail_radiogroup);
        maddress=(TextView)findViewById(R.id.address);
        back=(ImageView)findViewById(R.id.back);
        col=(ImageView)findViewById(R.id.master_col);
        videoFragment=new VideoFragment();
        askFragment=new AskFragment();
        fragmentManager=getSupportFragmentManager();
        transaction=fragmentManager.beginTransaction();
        transaction.add(R.id.id_content1,videoFragment);
        transaction.commit();


    }

    public void initEvents()
    {

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        col.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                collect();
            }
        });
    }


    private  void  collect(){


        if("no".equals(coresult)){
            MasterModel model = new MasterModel();
            model.colmaster(userid,id, masterCollectListener);
            getMasterState();
        } else{
            UserModel userModel = new UserModel();
            userModel.Nocollect(userid ,id,"2",usernocollectListener);
        }


    }
  MasterCollectListener masterCollectListener =new MasterCollectListener() {
    @Override
       public void onResponse(Integer listBean) {
        Toast.makeText(MasterDetailActivity.this,"关注成功",Toast.LENGTH_LONG).show();
        }

        @Override
        public void onFail(String msg) {

    }
};


    UserNoCollectListener usernocollectListener=new UserNoCollectListener() {
        @Override
        public void onResponse(Integer stringBean) {
            Toast.makeText(MasterDetailActivity.this,"取消关注成功",Toast.LENGTH_LONG).show();
            getMasterState();
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(MasterDetailActivity.this,"取消关注失败",Toast.LENGTH_LONG).show();
        }
    };



   private  void   getMasterState(){
       MasterModel masterModel =new MasterModel();
       masterModel.getState(id,userid,masterStateListener);
   }


    MasterStateListener masterStateListener =new MasterStateListener() {
        @Override
        public void onResponse(Integer listBean) {
            col.setImageResource(R.drawable.ic_heart);
            coresult = "yes";

        }

        @Override
        public void onFail(String msg) {
            col.setImageResource(R.drawable.ic_heart1);
            coresult = "no";

        }
    };

    public void initData(){

        realname=info.getRealname();
        thumb = info.getThumb();
        String s=info.getIntroduction().replaceAll("</p>","").replaceAll("<p>","");
        province_name=info.getCity_name()+info.getClub_name();;

        maddress.setText(province_name);
        name.setText(realname);
        intro.setText(s);
        if (thumb != "") {
            Picasso.with(MasterDetailActivity.this)
                    .load("http://172.24.10.175/workout/Uploads/" + thumb)
                    .into(pic);
            pic.setScaleType(ImageView.ScaleType.FIT_XY);
        } else {
            pic.setImageResource(R.drawable.qya);
        }

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                switch (checkedId)
                {
                    case  R.id.tea_detail_class:
                        replace(videoFragment);
                        break;
                    case R.id.tea_detail_ask:
                        replace(askFragment);
                        break;
                }
            }
        });

    }
    private void replace(Fragment fragment)
    {
        fragmentManager=getSupportFragmentManager();
        transaction=fragmentManager.beginTransaction();
        transaction.replace(R.id.id_content1,fragment);
        transaction.commit();
    }

}





